package com.atguigu.gulimall.product.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

//当ThreadPoolConfigProperties类上加了@Bean注解的时候,该注解可省略
//@EnableConfigurationProperties(ThreadPoolConfigProperties.class)
@Configuration
public class MyThreadPoolConfig {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties threadPoolConfigProperties){
        return new ThreadPoolExecutor(
                 threadPoolConfigProperties.getCoreThreadSize()  //核心线程池(一般50)
                ,threadPoolConfigProperties.getMaxThreadSize()    //最大线程池
                ,threadPoolConfigProperties.getKeepAliveTime()  //空闲线程多长时间关闭(10s)
                ,TimeUnit.SECONDS //时间单位(秒)
                ,new LinkedBlockingDeque<>(100000) //阻塞队列存储数量(也就是说最多存储10万个异步任务)
                ,Executors.defaultThreadFactory()  //线程工厂使用默认的线程工厂
                ,new ThreadPoolExecutor.AbortPolicy());  //拒绝策略(新的任务来了直接抛弃)
    }

}


















